package com.example.armmvvm.ui.constraint.scrollfallview

import android.view.View
import android.view.ViewGroup
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.example.arm.ext.dpInt
import com.example.armmvvm.R
import com.example.armmvvm.databinding.LayoutBottomBehaviorTest2Binding
import com.example.armmvvm.ui.fallview.FallViewMode
import com.example.armmvvm.ui.fallview.MiddleHeightCalc
import com.example.armmvvm.ui.fallview.BottomFallView
import com.example.armmvvm.ui.fallview.BottomFallViewSetting
import com.example.armmvvm.ui.viewpager.SimpleBackgroundFragment

/**
 *  author : yanghaozhang
 *  date : 2022/10/18 17:48
 *  description :
 */
class ViewPagerFallView(lifecycleOwner: LifecycleOwner, viewContainer: ViewGroup) :
    BottomFallView(lifecycleOwner, viewContainer, R.layout.layout_bottom_behavior_test2) {

    lateinit var binding: LayoutBottomBehaviorTest2Binding

    override fun updateView(view: View) {
        binding = LayoutBottomBehaviorTest2Binding.bind(view)
        binding.apply {
            scrollView.updateLayoutParams {
                height = 1000.dpInt
            }
//            root.setPadding(0, 0, 0, 30.dpInt)

            val fragmentList = listOf(
                SimpleBackgroundFragment("aa"),
                SimpleBackgroundFragment("bb"),
                SimpleBackgroundFragment("cc"),
                SimpleBackgroundFragment("dd"),
            )
            frameContent.adapter =
                ViewPagerAdapter(fragmentList, (lifecycleOwner as FragmentActivity).supportFragmentManager, lifecycleOwner.lifecycle)

            ivLeftArrow.setOnClickListener {
                val currentItem = frameContent.currentItem
                if (currentItem in 1 until fragmentList.size) {
                    frameContent.currentItem = currentItem - 1
                }
            }
            ivRightArrow.setOnClickListener {
                val currentItem = frameContent.currentItem
                if (currentItem in 0 until (fragmentList.size - 1)) {
                    frameContent.currentItem = currentItem + 1
                }
            }
        }
    }

    override fun bottomFallViewSetting(view: View): BottomFallViewSetting? {
        return BottomFallViewSetting(view).apply {
            mode = FallViewMode.THREE_STATE
            calcHeight = {
                peekHeight = 200.dpInt
            }
            expandedOffset = 30.dpInt
            middleHeightCalc = MiddleHeightCalc.withRatioMiddleHeight(0.5f)
        }
    }

    class ViewPagerAdapter(
        private val fragmentList: List<SimpleBackgroundFragment>,
        fragmentManager: FragmentManager,
        lifecycle: Lifecycle
    ) : FragmentStateAdapter(fragmentManager, lifecycle) {

        override fun getItemCount(): Int {
            return fragmentList.size
        }

        override fun createFragment(position: Int): Fragment {
            return fragmentList[position]
        }
/*
        override fun getItemId(position: Int): Long {
            return position.toLong()
        }*/
    }
}